package com.dc.business.datacollectbusiness.controller;

import com.dc.business.datacollectbusiness.entity.Company;
import com.dc.business.datacollectbusiness.entity.CompanyUserRelationShip;
import com.dc.business.datacollectbusiness.entity.User;
import com.dc.business.datacollectbusiness.model.Companyarr;
import com.dc.business.datacollectbusiness.model.Result;
import com.dc.business.datacollectbusiness.service.CompanyService;
import com.dc.business.datacollectbusiness.service.CompanyUserRelationShipService;
import com.dc.business.datacollectbusiness.service.UserService;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/company")
@CrossOrigin
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @Autowired
    private UserService userService;

    @Autowired
    private CompanyUserRelationShipService companyUserRelationShipService;

//    @GetMapping("/getall")
//    public Result getAllCompany() {
//        List<Company> companyList=companyService.getAllCompany();
//        if(companyList.isEmpty()){
//            return Result.error("company not found");
//        }else {
//            return Result.success(companyList);
//        }
//    }

    @DeleteMapping("/delete")
    public Result deleteCompany(@RequestParam int companyId) {
        boolean b=false;
        b=companyService.deleteCompanyById(companyId);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("删除失败");
        }
    }

    @PostMapping ("/insert")
    public Result insertCompany(@RequestBody Company company) {
        boolean b=false;
        b=companyService.insertCompany(company);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("插入失败");
        }
    }

    @PostMapping ("/update")
    public Result updateCompany(@RequestBody Company company) {
        boolean b=false;
        b=companyService.updateCompany(company);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("修改失败");
        }
    }

    @PostMapping("/getalltj")
    public Result getAllCompanyBytj(@RequestBody Company company) {
        try {
            Map<String,Object> map=new HashMap<>();
            map= ThreadLocalUtil.get();
            User userupdate=userService.findUserByUsername(map.get("username").toString());
            if (userupdate.getUserType().equals("超级管理员")){
                System.out.println("超级管理员");
                List<Company> companyList=companyService.getAllCompanyBytj(company);
                if(companyList.isEmpty()){
                    return Result.error("company not found");
                }else {
                    return Result.success(companyList);
                }
            }else {
                int userId=userupdate.getUserId();
                List<CompanyUserRelationShip> companyUserRelationShipList=companyUserRelationShipService.getAllCompanyIdByUserid(userId);
                if (companyUserRelationShipList.isEmpty()){
                    return Result.error("company not found");
                }else {
                    List<Company> companyList=new ArrayList<Company>();
                    for (CompanyUserRelationShip companyUserRelationShip:companyUserRelationShipList) {
                        int companyId= companyUserRelationShip.getCompanyId();
                        companyList.add(companyService.getAllCompanyByid(companyId));
                    }
                    return Result.success(companyList);
                }

            }

        }catch (Exception e){
            return Result.error(e.getMessage());
        }


    }

    @PostMapping("/deleteall")
    public Result deleteCompanyall(@RequestBody Companyarr companyarr) {
        boolean b=false;
        b=companyService.deleteCompanyByIdall(companyarr);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/getbyid")
    public Result getAllCompanyByid(@RequestParam int companyId) {
        Company company=companyService.getAllCompanyByid(companyId);
        if(company==null){
            return Result.error("company not found");
        }else {
            return Result.success(company);
        }
    }


    @GetMapping  ("/updateBid")
    public Result updateCompany(@RequestParam Integer userId,@RequestParam int companyId) {
        try {
            Map<String,Object> map=new HashMap<>();
            map= ThreadLocalUtil.get();
            User userupdate=userService.findUserByUsername(map.get("username").toString());
            if (!userupdate.getUserType().equals("超级管理员")){
                return Result.error("你不是超级管理员无法执行此操作");
            }else {
                boolean b=false;
                b=companyService.updateCompanyByUserId(userId,companyId);
                if(b!=false){
                    return Result.success(b);
                }else {
                    return Result.error("修改失败");
                }
            }

        }catch (Exception e){
            return Result.error(e.getMessage());
        }

    }


    @PostMapping("/generation")
    public Result generationParameters(@RequestBody Company company) {
        boolean b=false;
        b=companyService.generationParameters(company);
        if(b!=false){
            return Result.success(b);
        }else {
            return Result.error("生成失败");
        }
    }

}
